package com.system.connect.springboot_flowers.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.system.connect.springboot_flowers.dto.FlowersDto;
import com.system.connect.springboot_flowers.entity.Flowers;
import com.system.connect.springboot_flowers.service.IFlowersService;
import com.system.connect.springboot_flowers.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 花种表 前端控制器
 * </p>
 *
 * @author yang
 * @since 2025-01-13
 */
@RestController
@RequestMapping("/api/flowers")
public class FlowersController {

    @Autowired
    private IFlowersService flowersService;

    /**
     * 花种分类
     */
    @GetMapping
    public R getPostsByKeyPath(@RequestParam("keypath") String keyPath) {
        System.out.println("这是获取花种类型"+keyPath);
        if (keyPath != null){
            //枚举keyPath的值
            switch (keyPath) {
                case "1":
                    return R.success(flowersService.list());
                case "2,2-1":{//花期倒叙
                    QueryWrapper<Flowers> queryWrapper = new QueryWrapper<>();
                    queryWrapper.orderByDesc("bloom_period");
                    return R.success(flowersService.list(queryWrapper));
                }
                case "2,2-2":{//花期正序
                    QueryWrapper<Flowers> queryWrapper = new QueryWrapper<>();
                    queryWrapper.orderByAsc("bloom_period");
                    return R.success(flowersService.list(queryWrapper));
                }
                case "3,3-1": {//红色分类
                    QueryWrapper<Flowers> queryWrapper = new QueryWrapper<>();
                    queryWrapper.like("color", "红");
                    return R.success(flowersService.list(queryWrapper));
                }
                case "3,3-2":{//黄色分类
                    QueryWrapper<Flowers> queryWrapper = new QueryWrapper<>();
                    queryWrapper.like("color", "黄");
                    return R.success(flowersService.list(queryWrapper));
                }
                case "3,3-3":{//白色分类
                    QueryWrapper<Flowers> queryWrapper = new QueryWrapper<>();
                    queryWrapper.like("color", "白");
                    return R.success(flowersService.list(queryWrapper));
                }
                case "3,3-4":{//蓝色分类
                    QueryWrapper<Flowers> queryWrapper = new QueryWrapper<>();
                    queryWrapper.like("color", "蓝");
                    return R.success(flowersService.list(queryWrapper));
                }
                case "3,3-5":{//紫色分类
                    QueryWrapper<Flowers> queryWrapper = new QueryWrapper<>();
                    queryWrapper.like("color", "紫");
                    return R.success(flowersService.list(queryWrapper));
                }
                case "3,3-6":{//紫色分类
                    QueryWrapper<Flowers> queryWrapper = new QueryWrapper<>();
                    queryWrapper.like("color", "多");
                    return R.success(flowersService.list(queryWrapper));
                }
                case "4,4-1":{//新手友好型
                    QueryWrapper<Flowers> queryWrapper = new QueryWrapper<>();
                    queryWrapper.like("difficulty", "新手友好型");
                    return R.success(flowersService.list(queryWrapper));
                }
                case "4,4-2":{//中等维护需求
                    QueryWrapper<Flowers> queryWrapper = new QueryWrapper<>();
                    queryWrapper.like("difficulty", "中等维护需求");
                    return R.success(flowersService.list(queryWrapper));
                }
                case "4,4-3":{//高级养护需求
                    QueryWrapper<Flowers> queryWrapper = new QueryWrapper<>();
                    queryWrapper.like("difficulty", "高级养护需求");
                    return R.success(flowersService.list(queryWrapper));
                }
                case "5,5-1":{//吸附甲醛花卉
                    QueryWrapper<Flowers> queryWrapper = new QueryWrapper<>();
                    queryWrapper.like("special_attribute", "吸附甲醛花卉");
                    return R.success(flowersService.list(queryWrapper));
                }
                case "5,5-2":{//抗污染花卉
                    QueryWrapper<Flowers> queryWrapper = new QueryWrapper<>();
                    queryWrapper.like("special_attribute", "吸附抗污染花卉");
                    return R.success(flowersService.list(queryWrapper));
                }
                case "5,5-3":{//耐阴花卉
                    QueryWrapper<Flowers> queryWrapper = new QueryWrapper<>();
                    queryWrapper.like("special_attribute", "耐阴花卉");
                    return R.success(flowersService.list(queryWrapper));
                }
                case "5,5-4":{//耐旱花卉
                    QueryWrapper<Flowers> queryWrapper = new QueryWrapper<>();
                    queryWrapper.like("special_attribute", "耐旱花卉");
                    return R.success(flowersService.list(queryWrapper));
                }
                case "5,5-5":{//耐寒花卉
                    QueryWrapper<Flowers> queryWrapper = new QueryWrapper<>();
                    queryWrapper.like("special_attribute", "耐寒花卉");
                    return R.success(flowersService.list(queryWrapper));
                }
                case "6,6-1":{//圣诞节花卉
                    QueryWrapper<Flowers> queryWrapper = new QueryWrapper<>();
                    queryWrapper.like("category", "圣诞节花卉");
                    return R.success(flowersService.list(queryWrapper));
                }
                case "6,6-2":{//情人节花卉
                    QueryWrapper<Flowers> queryWrapper = new QueryWrapper<>();
                    queryWrapper.like("category", "情人节花卉");
                    return R.success(flowersService.list(queryWrapper));
                }
                case "6,6-3":{//新年花卉
                    QueryWrapper<Flowers> queryWrapper = new QueryWrapper<>();
                    queryWrapper.like("category", "新年花卉");
                    return R.success(flowersService.list(queryWrapper));
                }
                case "6,6-4":{//其他传统节日花卉
                    QueryWrapper<Flowers> queryWrapper = new QueryWrapper<>();
                    queryWrapper.like("category", "其他传统节日花卉");
                    return R.success(flowersService.list(queryWrapper));
                }
                case "7,7-1":{//爱情与浪漫
                    QueryWrapper<Flowers> queryWrapper = new QueryWrapper<>();
                    queryWrapper.like("tags", "爱情与浪漫");
                    return R.success(flowersService.list(queryWrapper));
                }
                case "7,7-2":{//友谊与感谢
                    QueryWrapper<Flowers> queryWrapper = new QueryWrapper<>();
                    queryWrapper.like("tags", "友谊与感谢");
                    return R.success(flowersService.list(queryWrapper));
                }
                case "7,7-3":{//祝福与好运
                    QueryWrapper<Flowers> queryWrapper = new QueryWrapper<>();
                    queryWrapper.like("tags", "祝福与好运");
                    return R.success(flowersService.list(queryWrapper));
                }
                case "7,7-4":{//长寿与健康
                    QueryWrapper<Flowers> queryWrapper = new QueryWrapper<>();
                    queryWrapper.like("tags", "长寿与健康");
                    return R.success(flowersService.list(queryWrapper));
                }
                case "7,7-5":{//荣誉与成就
                    QueryWrapper<Flowers> queryWrapper = new QueryWrapper<>();
                    queryWrapper.like("tags", "荣誉与成就");
                    return R.success(flowersService.list(queryWrapper));
                }
                case "7,7-6":{//纪念与哀悼
                    QueryWrapper<Flowers> queryWrapper = new QueryWrapper<>();
                    queryWrapper.like("tags", "纪念与哀悼");
                    return R.success(flowersService.list(queryWrapper));
                }
                case "7,7-7":{//家庭与亲情
                    QueryWrapper<Flowers> queryWrapper = new QueryWrapper<>();
                    queryWrapper.like("tags", "家庭与亲情");
                    return R.success(flowersService.list(queryWrapper));
                }
            }
        }
        return R.success(keyPath);
    }

    /**
     * 新建花种信息
     */
    @PostMapping("/create")
    public R create(@RequestBody FlowersDto flowersDto) {

        System.out.println(flowersDto);
        if (flowersService.create(flowersDto))
          return R.success("新建花种信息成功");
        return R.error("新建花种信息失败");
    }

    /**
     * 获取所有花种信息
     */
    @GetMapping("/list")
    public R list() {
        return R.success(flowersService.list());
    }

    /**
     * 获取花种详情
     */
    @GetMapping("/{flower_id}") // 确保路径与前端请求匹配
    public R getPostDetail(@PathVariable("flower_id") Integer flower_id) {
        return R.success(flowersService.getById(flower_id));
    }

    /**
     * 获取所有花种列表（管理员接口）
     */
    @GetMapping("/admin/list")
    public R getAllFlowers() {
        List<Flowers> flowers = flowersService.list();
        return R.success(flowers);
    }

    /**
     * 创建新花种（管理员接口）
     */
    @PostMapping("/admin/create")
    public R createFlower(@RequestBody Flowers flower) {
        boolean result = flowersService.save(flower);
        return result ? R.success("创建花种成功") : R.error("创建花种失败");
    }

    /**
     * 更新花种信息（管理员接口）
     */
    @PutMapping("/admin/update/{id}")
    public R updateFlower(@PathVariable("id") Integer id, @RequestBody Flowers flower) {
        flower.setFlowerId(id);
        boolean result = flowersService.updateById(flower);
        return result ? R.success("更新花种成功") : R.error("更新花种失败");
    }

    /**
     * 删除花种（管理员接口）
     */
    @DeleteMapping("/admin/delete/{id}")
    public R deleteFlower(@PathVariable("id") Integer id) {
        boolean result = flowersService.removeById(id);
        return result ? R.success("删除花种成功") : R.error("删除花种失败");
    }

    /**
     * 获取花种详情（管理员接口）
     */
    @GetMapping("/admin/detail/{id}")
    public R getFlowerDetail(@PathVariable("id") Integer id) {
        Flowers flower = flowersService.getById(id);
        return flower != null ? R.success(flower) : R.error("花种不存在");
    }

    /**
     * 批量更新花种状态（管理员接口）
     */
    @PutMapping("/admin/updateStatus")
    public R updateFlowersStatus(@RequestBody List<Integer> ids, @RequestParam Boolean status) {
        QueryWrapper<Flowers> wrapper = new QueryWrapper<>();
        wrapper.in("flower_id", ids);
        Flowers flower = new Flowers();
        flower.setStatus(status);
        boolean result = flowersService.update(flower, wrapper);
        return result ? R.success("更新状态成功") : R.error("更新状态失败");
    }

    /**
     * 批量更新花种库存（管理员接口）
     */
    @PutMapping("/admin/updateStock")
    public R updateFlowersStock(@RequestBody List<Integer> ids, @RequestParam Integer stock) {
        QueryWrapper<Flowers> wrapper = new QueryWrapper<>();
        wrapper.in("flower_id", ids);
        Flowers flower = new Flowers();
        flower.setStock(stock);
        boolean result = flowersService.update(flower, wrapper);
        return result ? R.success("更新库存成功") : R.error("更新库存失败");
    }
}
